package com.cet.qz.unit01.chapter1_2;

import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.StdIn;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-02-26 22:14
 **/
public class Qz1_2_2 {

    public static Interval1D[] genIntervalArray(int N) {

        System.out.println("请输入"+N+"个间隔(先输入下界，后输入上界)");
        Interval1D[] interval1DS = new Interval1D[N];
        for (int i = 0; i < N; i++) {
            double min = StdIn.readDouble();
            double max = StdIn.readDouble();
            interval1DS[i] = new Interval1D(min, max);
        }

        return interval1DS;
    }

    public static void printIntersectedIntervals(Interval1D[] interval1DS) {
        int length = interval1DS.length;
        for (int i = 0; i < length; i++) {
            for (int j = i+1; j < length; j++) {
                if (interval1DS[i].intersects(interval1DS[j])) {
                    System.out.println(interval1DS[i]+"<=====相交=====>"+interval1DS[j]);
                }
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("请输入生成的间隔数：");
        int N = StdIn.readInt();
        Interval1D[] interval1DS = genIntervalArray(N);
        printIntersectedIntervals(interval1DS);
    }
}
